﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using DevExpress.XtraGrid.Views.Grid;

namespace EFong.View.BankAccount
{
    public partial class Frm_BankAccount : BasicMdiChildForm
    {

        // PRIVATE :  MEMEBERS ::::::::::::::::::::::::::::::::::::::::: //

        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}
        private Proxy.BaseServiceProxy _baseProxy {get{ return Proxy.BaseServiceProxy.GetInstance();}}

        public Frm_BankAccount()
        {
            InitializeComponent();
            this.gv_BankAccounts.OptionsView.NewItemRowPosition = NewItemRowPosition.Top;
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        protected override void InitFields()
        {
            base.InitFields();
        }

        protected override void Events()
        {
            this.gv_BankAccounts.ShowingEditor += (s, e) =>
                {
                    Contract.BankAccount bank = this.gv_BankAccounts.GetRow(this.gv_BankAccounts.FocusedRowHandle) as Contract.BankAccount;
                    if (bank == null)
                        return;
                    if (bank.IsNew)
                        return;

                    if (this.gv_BankAccounts.FocusedColumn == colAmount || this.gv_BankAccounts.FocusedColumn == colCurrency)
                    {
                        foreach (Contract.RoleAuthorizedItem roleAuthorizedItem in AppParameters.LoginUser.Role.Authorities)
                        {
                            if (roleAuthorizedItem.AuthorizedItem.Name == "高级修改")
                            {
                                if (roleAuthorizedItem.Allow)
                                {
                                    if (this.gv_BankAccounts.FocusedColumn == colCurrency)
                                        e.Cancel = true;
                                }
                                else
                                        e.Cancel = true;
                            }
                        }
                    }
                };
        }

        protected override void LoadDatas()
        {
            base.LoadDatas();
            this.bds_bankAccount.BindingData(this._masterProxy.GetAllBankAccount().ReturnValue);
            this.repositoryItemComboBox1.BindingData(this._baseProxy.GetAllCurrency().ReturnValue);
        }

        private void save()
        {
            UIOperManager.SaveData(
                            () =>
                                {
                                    return true;
                                },
                                () =>
                                {
                                    this.gv_BankAccounts.PostEditor();

                                    return _masterProxy.UpdateBankAccounts( (List<Contract.BankAccount>)this.bds_bankAccount.DataSource ).IsSuccess;
                                },
                                MsgResource.Save_Complete,
                                () =>
                                {
                                    DialogResult = DialogResult.OK;
                                    this.Close();
                                }
                            );
        }

    }



}
